@echo off

set cur_path=%~dp0

SETLOCAL enabledelayedexpansion

:main
call :check_tool
call :build_code
exit

:check_tool
for /f "tokens=1 delims=" %%i in ('plink -V') do set plink_result=%%i
if "%plink_result%"=="" (
    echo Not found plink,Please download putty or plink!
    echo https://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html
    call :quit
)
for /f "tokens=1 delims=" %%i in ('pscp -V') do set pscp_result=%%i
if "%pscp_result%"=="" (
    echo Not found plink,Please download putty or plink!
    echo https://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html
    call :quit
)
exit /B 0

:build_code
if exist %cur_path%my.ini (
    set index=0
    for /f "tokens=1 delims=" %%i in (my.ini) do (
        set config[!index!]=%%i
        set /a index=!index!+1
    )
    set name=!config[0]:~0,-1!
    set host=!config[1]:~0,-1!
    set password=!config[2]:~0,-1!
) else (
    echo Please input username
    set /p name=
    echo !name! >> %cur_path%my.ini
    echo Please input hostname
    set /p host=
    echo !host! >> %cur_path%my.ini
    echo Please input password
    set /p password=
    echo !password! >> %cur_path%my.ini
)
(
echo if [ -e "/home/%name%/OpenHarmony/master" ]; then
echo     rm -rf /home/%name%/OpenHarmony/master
echo fi
echo mkdir -p /home/%name%/OpenHarmony/master
echo cd /home/%name%/OpenHarmony/master
echo repo init -u https://gitee.com/openharmony/manifest.git -b master --no-repo-verify
echo repo sync -c
echo repo forall -c 'git lfs pull'
echo sh ./build/prebuilts_download.sh ) > build_dos.sh
for /f "tokens=1 delims=" %%i in ('pscp -pw %password% -r .\build_dos.sh %name%@%host%:/home/%name%/') do set pscp_result=%%i
if not "!pscp_result:~-4,-1!" equ "100" (
    echo build.sh send fail!
    call :quit
)
plink %name%@%host% -pw %password% "sed -e 's/.$//' build_dos.sh > build.sh; rm build_dos.sh"
del build_dos.sh
for /f "tokens=1 delims=" %%i in ('plink %name%@%host% -pw %password% sh ./build.sh') do echo %%i
exit /B 0

:quit
pause
exit
goto :EOF















